/**
 * 
 */
package com.gr.staffpm.pages.dashboard;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.protocol.http.WebRequestCycle;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @author Graham Rhodes 12 Mar 2011 15:38:09
 */
public class GadgetContainer extends Panel {

    private static final long serialVersionUID = 1L;
    private static final double WIDTH_OFFSET = 1.147915;
    private static final double WIDTH = 31;
    private int initialWidthOffset = 2;
    private final RepeatingView gadgets = new RepeatingView("gadgets");
    private final Logger log = LoggerFactory.getLogger(getClass());

    public enum ALIGNMENT {
        LEFT, CENTER, RIGHT;
    }

    public GadgetContainer(String id) {
        super(id);
        WebClientInfo clientInfo = (WebClientInfo) WebRequestCycle.get().getClientInfo();
        log.debug("Browser: " + clientInfo.getUserAgent());
        if (clientInfo.getUserAgent().contains("MSIE"))
            initialWidthOffset = 5;
    }

    public void addGadget(String label, Component content, ALIGNMENT alignment, int colour) {
        switch (alignment) {
            case LEFT:
                addLeftGadget(label, content, colour);
                break;
            case CENTER:
                addCenterGadget(label, content, colour);
                break;
            case RIGHT:
                addRightGadget(label, content, colour);
                break;
        }
        addOrReplace(gadgets);
    }

    private void addLeftGadget(String label, Component content, final int colour) {
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + initialWidthOffset + "%; width: " + WIDTH + "%;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color" + colour + " left-gadget");
            }
        };
        gadgets.add(gadget);
    }

    private void addCenterGadget(String label, Component content, final int colour) {
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + (initialWidthOffset + WIDTH + WIDTH_OFFSET) + "%; width: " + WIDTH + "%;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color" + colour + " center-gadget");
            }
        };
        gadgets.add(gadget);
    }

    private void addRightGadget(String label, Component content, final int colour) {
        Gadget gadget = new Gadget(String.valueOf(gadgets.size()), label, content) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                super.onComponentTag(tag);
                String style = "left: " + (initialWidthOffset + (WIDTH + WIDTH_OFFSET) * 2) + "%; width: " + WIDTH + "%;";
                tag.getAttributes().put("style", style);
                tag.getAttributes().put("class", "gadget color" + colour + " right-gadget");
            }
        };
        gadgets.add(gadget);
    }

}
